package common;

import java.util.Collection;
import ConnDatabase.*;
import java.sql.*;
import java.util.*;


public class ShouldRentPartTag extends PartTag {
	
	private String section;
	private String building;
	private String room;
	private String year;
	private String month;
	private String get;
	
	protected void initParamPrefix() {
   		 prefix = "room_";
  	}
  	
  	public void setSection(String sectionTemp) {
  		section = sectionTemp;
  	}	
  	
  	public void setBuilding(String buildTemp) {
  		building = buildTemp;
  	}
  	
  	public void setRoom(String setValue) {
  		room = setValue;
  	}
  	
  	public void setYear(String setValue) {
  		year = setValue;
  	}			
  	
  	public void setMonth(String setValue) {
  		month = setValue;
  	}
  	
  	public void setGet(String setValue) {
  		get = setValue;
  	}		
  	
	//protected Collection findCollection() throws Exception {
	protected SelectedPart findPart() throws Exception {
		
        	System.out.println("rentparttag findpart ");
        	//RoomList roomList = new RoomList();
        	//SelectedPart  part = null;
        	//Collection rooms = null;
       	        //part = roomList.getRooms(pageIndex,10,section,building);    //pageindex = 1 , pagesize =10
                //return part;
                String sectionStr = "";
                String buildStr = "";
                String roomStr = "";
                String yearStr = "";
                String monthStr = "";
                String getStr = "";
                if (!section.equals(""))
                	sectionStr = " and h.section='" + section + "' ";
                if (!building.equals(""))
                	buildStr = " and h.building like '%" + building + "%' ";
                if (!room.equals(""))
                	roomStr = " and h.room like '%" + room + "%' " ;
                if (!year.equals(""))
                	yearStr = " and h.year=" + year + " ";	
                if (!month.equals(""))
                	monthStr = " and h.month=" + month + " ";	
                if (get.equals("1"))
                	getStr = " ";
                else if (get.equals("2"))
                	getStr = " and gets=1 ";
                else if (get.equals("3")) {
                	getStr = " and gets=0 ";	
                	monthStr = " and ((h.month<=" + month + " and h.year=" + year  + ") or h.year<" + year + ")";			
                }	
                System.out.print("section : " + section);
                String rentStr ;
                rentStr = "select h.id,s.serialno section,s.name sectionname,h.building,";
                rentStr += "h.room,c.signatory customers,h.year,h.month,h.money,";
                rentStr += "h.handler,h.handledate,h.gets";
                rentStr += " from hire_shouldget h,customer c,section s";
                rentStr += " where h.section=s.serialno and h.customer=c.serialno ";
                rentStr += sectionStr + buildStr + roomStr + yearStr + monthStr + getStr;
                rentStr += " and h.active=1";
                rentStr += " order by h.section,building,room,year desc,month desc";
                
                String countStr ;
                countStr = "select count(*) ";
                countStr += " from hire_shouldget h,customer c,section s";
                countStr += " where h.section=s.serialno and h.customer=c.serialno ";
                countStr += sectionStr + buildStr + roomStr + yearStr + monthStr + getStr;
                countStr += " and h.active=1";
                
                
                System.out.println(rentStr);
                SelectedPart sp = null;
						
		int factCount = 0;
		//int pageIndex = 1;
		int pageSize = 10;
		int skipRents = (pageIndex - 1) * pageSize;
		int pageSize2 = pageSize; //pageSize is all;
		System.out.println("pageIndex : " + pageIndex);
		int allRents = 0;
		
		ArrayList al = new ArrayList();
		
		ResultSet rentRS = null;
		
		try {
			System.out.println("-----begin connection");
			ConnDatabase conn = new ConnDatabase();
			System.out.println("----already connect");
			ResultSet rs2 = conn.executeQuery(countStr);
			if (rs2.next())
				allRents = rs2.getInt(1);
			rs2.close();
			System.out.println("allrents : " + allRents);	
			rentRS = conn.executeQuery(rentStr);
			System.out.println("skiprooms : " + skipRents);
			while (skipRents -- > 0 && rentRS.next()) ;
			
			System.out.println("pageSize : " + pageSize);
			while (pageSize2-- >0 && rentRS.next()) {
				factCount ++;
				int id = rentRS.getInt("id");
				String section = rentRS.getString("section");
				String sectionName = rentRS.getString("sectionName");
				String building = rentRS.getString("building");
				String room = rentRS.getString("room");
				String customer = rentRS.getString("customers");
				int year = rentRS.getInt("year");
				int month = rentRS.getInt("month");
				float money = rentRS.getFloat("money");
				String handler = rentRS.getString("handler");
				String handleDate = rentRS.getString("handleDate");
				int gets = rentRS.getInt("gets") ;
				System.out.println("customer : " + customer);
				
				
				ShouldRentDetail srd = new ShouldRentDetail(id,section,sectionName,
							building,room,customer,year,month,
							money,handler,handleDate,gets);
				
				al.add(srd);	
				System.out.println("------" + year + "----" + month );			
			}
		} catch (SQLException se) {
			System.out.println("Rent List : " + se.getMessage());
			throw new RoomException("sqlexception while get rent detail : " +
							se.getMessage());
		} finally {
			try {
				if (rentRS != null)
					rentRS.close();
			} catch (Exception e) {
				System.out.print("can't close roomRS in HirableRoomList");
			}	
		}	
		// get selected part
		sp = new SelectedPart(allRents,pageIndex,pageSize,
							factCount,al);
		return sp;								
			
	}	
}	